package guoanmaker.personal.business.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.service.PersonalInvoiceService;
import guoanmaker.personal.helper.common.AjaxJson;
import guoanmaker.personal.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;
/**
 * 
* @ClassName: PersonalInvoiceController 
* @Description: 发票controller
* @author Yvonne_MGL
* @date 2017年7月6日 下午3:34:57 
*  
 */
@RestController
@RequestMapping(ApiController.INVOICE_URL)
public class PersonalInvoiceController {
	
	@Resource
	private PersonalInvoiceService personalInvoiceService;
	
	
	@Autowired  
	private  HttpServletRequest request;
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param user_id 用户id
	* @date 2017年7月6日 下午3:35:17     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "保存发票接口", notes = "保存发票")
	@RequestMapping(value = "/saveInvoice", method = RequestMethod.POST)
	public AjaxJson saveInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,@RequestParam(required = false)MultipartFile businessLicenseFile,@RequestParam(required = false)MultipartFile businessFile,String isdefault,String user_id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.saveInvoice(invoiceName, taxNumber, invoiceType, telephone, address, bankName, bankNumber, businessLicenseFile, businessFile, isdefault, user_id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param user_id 用户id
	* @date 2017年7月6日 下午3:35:17     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "保存发票接口", notes = "保存发票")
	@RequestMapping(value = "/saveInvoiceForPC", method = RequestMethod.POST)
	public AjaxJson saveInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,String businessLicenseFile,String businessFile,String isdefault,String user_id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.saveInvoiceForPC(invoiceName, taxNumber, invoiceType, telephone, address, bankName, bankNumber, businessLicenseFile, businessFile, isdefault, user_id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}

	/**
	 * 
	* @Description: 删除发票
	* @param id 发票id
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:37:11     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "删除发票接口", notes = "删除发票")
	@RequestMapping(value = "/deleteInvoice", method = RequestMethod.POST)
	public AjaxJson deleteInvoice(String id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.deleteInvoice(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param id 发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午2:03:57     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "修改发票接口", notes = "修改发票")
	@RequestMapping(value = "/updateInvoice", method = RequestMethod.POST)
	public AjaxJson updateInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,@RequestParam(required = false)MultipartFile businessLicenseFile,@RequestParam(required = false)MultipartFile businessFile,String isdefault,String id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.updateInvoice(invoiceName, taxNumber, invoiceType, telephone, address, bankName, bankNumber, businessLicenseFile, businessFile, isdefault, id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param id 发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午2:03:57     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "修改发票接口", notes = "修改发票")
	@RequestMapping(value = "/updateInvoiceForPC", method = RequestMethod.POST)
	public AjaxJson updateInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,String businessLicenseFile,String businessFile,String isdefault,String id) {
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=personalInvoiceService.updateInvoiceForPC(invoiceName, taxNumber, invoiceType, telephone, address, bankName, bankNumber, businessLicenseFile, businessFile, isdefault, id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		
		return ajaxJson;
	}

}
